package com.apache.auditRule.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultMsg;
import com.apache.auditRule.entity.AuditRule;
import com.apache.auditRule.manager.AuditRuleManager;
import com.apache.cache.util.Validator;
import com.apache.client.UctOldCoreClient;
import com.apache.database.model.Page;
import com.apache.member.common.ConstantData;
import com.apache.uct.common.Root;
import com.apache.uct.common.entity.User;

/**
 * 审核规则控制器
 * description:  
 */
@Controller
@RequestMapping("/audit/auditRule")
public class AuditRuleController {

	@Autowired
	private AuditRuleManager auditRuleManager;

	@RequestMapping("/edit.action")
	@ResponseBody
	public ResultMsg edit(AuditRule auditRule) {

		ResultMsg resultMsg = new ResultMsg();
		resultMsg.setFlag(ConstantData.FLAG_FALSE);
		resultMsg.setMsg("操作失败");
		ParamsVo<AuditRule> vo = new ParamsVo<AuditRule>();
		vo.setObj(auditRule);
		boolean editInfo = auditRuleManager.editInfo(vo);
		if (editInfo) {
			resultMsg.setFlag(ConstantData.FLAG_OK);
			resultMsg.setMsg("操作成功");
		}

		return resultMsg;
	}

	@RequestMapping("/editActOrUser.action")
	@ResponseBody
	public ResultMsg editActOrUser(AuditRule auditRule, String userValue, String roleValue, String typeValue,
			HttpServletRequest request, HttpServletResponse response, String flag) {
		ResultMsg resultMsg = new ResultMsg();
		resultMsg.setFlag(ConstantData.FLAG_FALSE);
		resultMsg.setMsg("操作失败");
		ParamsVo<AuditRule> vo = new ParamsVo<AuditRule>();
		boolean editInfo = false;
		vo.setObj(auditRule);
		if ("edit".equals(flag)) {
			editInfo = auditRuleManager.editInfo(vo);
		} else {
			if (Validator.isNotNull(auditRuleManager.saveInfo(vo))) {
				editInfo = true;
			}
		}
		if (editInfo) {
			resultMsg.setFlag(ConstantData.FLAG_OK);
			resultMsg.setMsg("操作成功");
		}
		return resultMsg;
	}

	/**
	 * 审核规则修改回显
	 * description:  
	 * @param typeNo
	 * @return  
	 * @author lxsong
	 * @update 2015-6-24
	 */
	@RequestMapping("/toEdit.action")
	@ResponseBody
	public Map<String, Object> toEdit(String ruleId) {

		ParamsVo<AuditRule> vo = new ParamsVo<AuditRule>();
		vo.setInfoId(ruleId);
		AuditRule auditRule = (AuditRule) auditRuleManager.getInfoById(vo);
		Map<String, Object> mapData = new HashMap<String, Object>();
		mapData.put("auditRule", auditRule);
		return mapData;
	}

	/**
	 * 删除审核规则
	 * description:  
	 * @param ruleId
	 * @return  
	 * @author lxsong
	 * @update 2015-6-24
	 */
	@RequestMapping("/delete.action")
	@ResponseBody
	public ResultMsg delete(String ruleId) {
		ResultMsg resultMsg = new ResultMsg();
		resultMsg.setFlag(ConstantData.FLAG_FALSE);
		resultMsg.setMsg("操作失败");
		ParamsVo<AuditRule> vo = new ParamsVo<AuditRule>();
		vo.setInfoId(ruleId);
		boolean deleteInfo = auditRuleManager.deleteInfo(vo);
		if (deleteInfo) {
			resultMsg.setFlag(ConstantData.FLAG_OK);
			resultMsg.setMsg("操作成功");
		}
		return resultMsg;
	}

	/**
	 * 保存 审核规则
	 * description:  
	 * @param auditRule
	 * @param request
	 * @param response
	 * @return  
	 * @author lxsong
	 * @update 2015-6-24
	 */
	//@Token(remove = true)
	//防表单重复提交注解
	@RequestMapping("/save.action")
	@ResponseBody
	public ResultMsg save(AuditRule auditRule, HttpServletRequest request, HttpServletResponse response) {
		/*LoginUser loginUser = PBOSSOTools.getLoginUser(request);*/
		ResultMsg resultMsg = new ResultMsg();
		resultMsg.setFlag(ConstantData.FLAG_FALSE);
		resultMsg.setMsg("操作失败");
		ParamsVo<AuditRule> vo = new ParamsVo<AuditRule>();
		vo.setObj(auditRule);
		String saveInfo = auditRuleManager.saveInfo(vo);
		if (Validator.isNotNull(saveInfo)) {
			resultMsg.setFlag(ConstantData.FLAG_OK);
			resultMsg.setMsg("操作成功");
		}
		return resultMsg;
	}

	/**
	 * 审核规则列表
	 * description:  
	 * @param auditRule
	 * @param request
	 * @param page
	 * @param rows
	 * @return  
	 * @author lxsong
	 * @update 2015-6-24
	 */
	@RequestMapping("/list.action")
	@ResponseBody
	public Map<String, Object> list(AuditRule auditRule, HttpServletRequest request, String page, String rows) {

		ParamsVo<AuditRule> vo = new ParamsVo<AuditRule>();
		vo.setParams("pageSize", Validator.getDefaultStr(rows, "5"));
		vo.setParams("pageIndex", Validator.getDefaultStr(page, "1"));

		vo.setObj(auditRule);
		Page pageList = auditRuleManager.getPageInfo(vo);
		Map<String, Object> mapData = new HashMap<String, Object>();
		mapData.put("total", pageList.getCount());
		mapData.put("rows", pageList.getPageObjects());
		return mapData;
	}

	/**
	 * 根据roleId 拿到所有的用户
	 * description:  
	 * @param id
	 * @return  
	 * @author yaojs 
	 * @update 2015-7-17
	 */
	@RequestMapping("/searchUserByActId.action")
	@ResponseBody
	public String searchUserByActId(String id) {
		Map<String, String> paramMap = new HashMap<String, String>();
		paramMap.put("userVo.actId", id);
		paramMap.put("userVo.delStatus", "F");
		//		Root root = UctCoreClient.searchUserByUserVo(paramMap);
		Root root = UctOldCoreClient.searchUserByUserVo(paramMap);
		Page userPage = (Page) root.getEntity();
		List<User> list = userPage.getPageObjects();
		if (Validator.isEmpty(list)) {
			return null;
		} else {
			StringBuffer userEnames = new StringBuffer("");
			for (int i = 0; i < list.size(); i++) {
				userEnames.append(list.get(i).getUserEname()).append(",");
			}
			String checkUser = userEnames.toString();
			checkUser = checkUser.substring(0, checkUser.length() - 1);
			return checkUser;
		}
	}

}
